package com.aier.admin.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.eicp.ghostboy.futil.MD5Util;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin.service.IAdminUserService;
import com.aier.common.common.CommonSession;
import com.aier.common.common.Constant;
import com.aier.proxy.entity.Proxy;
import com.aier.proxy.service.IProxyService;
import com.aier.user.common.UserFinalData;
import com.aier.user.entity.User;
import com.aier.user.service.IUserService;

@Scope("prototype")
@Component("adminLoginAction")
public class AdminLoginAction extends AdminBaseAction {

    /**
     * 管理员和代理商登陆
     */
    // 账号
    public String account;
    // 密码
    public String pwd;

    public int s;
    private static final long serialVersionUID = 1L;

    @Resource(name = "adminUserService")
    private IAdminUserService adminUserService;
    @Resource(name = "userService")
    private IUserService userService;
    @Resource(name = "proxyService")
    private IProxyService proxyService;

    // 代理商用户
    private Integer pt;
    // 代理商id与名称
    private List<String[]> tl;
    // 代理商id
    private Integer pid;

    
    @Override
    public String execute() {

        return SUCCESS;
    }

    /**
     * 执行后台管理员登陆
     * 
     * @return
     */
    public String adminLogin() {
        s = 0;
        // 判断是否为管理员用户
        if (adminUserService.searchAdminUser(account) != null) {

            User user = userService.searchUserByCondition(account,
                    MD5Util.getMD5(pwd));// 密码验证
            if (user != null) {
                session.put(CommonSession.SESSION_ACCOUNT, account);
                session.put(CommonSession.SESSION_ADMINACCOUNT,
                        user.getAccount());
                session.put(CommonSession.SESSION_ACCOUNT_ADMINID,
                        user.getUid());
                s = Constant.num_1001;
            }
        } else {
            s = Constant.num_1000;
        }
        // s = 1;
        return SUCCESS;
    }

    /**
     * 代理商登陆
     * 
     * @param account
     */
    public String proxyLogin() {
        if (null != userService.searchUserByCondition(account)) {
            User user = userService.searchUserByCondition(account,
                    MD5Util.getMD5(pwd));
            if (null != user) {
                // 判断是否是代理商
                List<Proxy> lp = proxyService.searchProxyAndCityByUid(user
                        .getUid());
                if (null != lp && lp.size() > 0) {
                    user.setIdenProxy(UserFinalData.IDENTITY_PROXY_YES);
                    tl = new ArrayList<String[]>();
                    for (Proxy p : lp) {
                        String[] ts = new String[Constant.num_3];
                        ts[0] = String.valueOf(p.getPid());
                        ts[1] = p.getName();
                        ts[2] = String.valueOf(p.getProxyCity().getCid());
                        tl.add(ts);
                    }
                    user.setIdenPL(tl);
                    session.put(CommonSession.SESSION_ACCOUNT, account);
                    session.put(CommonSession.SESSION_ACCOUNT_ID, user.getUid());
                    session.put(CommonSession.SESSION_ACCOUNT_NAME,
                            user.getAccount());
                    // 设置代理商身份
                    session.put(CommonSession.SESSION_ACCOUNT_IDENTITY_PROXY,
                            user.getIdenProxy());
                    session.put(
                            CommonSession.SESSION_ACCOUNT_IDENTITY_PROXY_LIST,
                            user.getIdenPL());
                    // 是代理商
                    pt = UserFinalData.IDENTITY_PROXY_YES;
                    pid = Integer.valueOf(tl.get(0)[0]);
                    s = Constant.num_1001;
                }
            }
        } else {
            s = Constant.num_1000;
        }
        return SUCCESS;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public int getS() {
        return s;
    }

    public void setAdminUserService(IAdminUserService adminUserService) {
        this.adminUserService = adminUserService;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public Integer getPt() {
        return pt;
    }

    public List<String[]> getTl() {
        return tl;
    }

    public Integer getPid() {
        return pid;
    }

    public void setProxyService(IProxyService proxyService) {
        this.proxyService = proxyService;
    }

}
